<template>
    <div class="app-container">
        <el-form :inline="true" :model="queryParams" ref="queryForm"
                 style="text-align: left" v-show="showSearch">
            <el-form-item label="方案名" prop="userPhone">
                <el-input
                        @keyup.enter.native="handleQuery"
                        clearable
                        placeholder="请输入方案名查询"
                        style="width: 240px"
                        v-model="queryParams.dietName"
                />
            </el-form-item>
            <el-form-item>
                <el-button @click="handleQuery" type="warning"><el-icon><Search /></el-icon>搜索</el-button>
                <el-button @click="resetQuery"><el-icon><Refresh /></el-icon>重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8" style="margin: 10px 0px">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        @click="addButton()"
                ><el-icon><EditPen /></el-icon>新增方案</el-button>
            </el-col>
        </el-row>

        <el-card class="box-card">
            <template #header>
                <div class="card-header">
                    <span>饮食方案制定</span>
                    <el-button class="button" text>Physical Examination Appointment Records</el-button>
                </div>
            </template>
            <el-table :data="list">
                <el-table-column align="center" type="index" width="50"/>
                <el-table-column align="center" label="档案编号" prop="archiveNumber"/>
                <el-table-column align="center" label="方案名称" prop="dietName"/>
                <el-table-column align="center" label="方案内容" prop="dietContent"/>
                <el-table-column align="center" label="方案制定时间" prop="createTime">
                    <template #default="scope">
                        <span>{{ $filters.formatDate(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="方案更新时间" prop="createTime">
                    <template #default="scope">
                        <span>{{ $filters.formatDate(scope.row.updateTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        label="操作"
                        width="160"
                >
                    <template #default="scope">
                        <el-button
                                @click="handleUpdate(scope.row)"
                                :link="true"
                                style="color: #266fff"
                        ><el-icon><Edit /></el-icon>更新
                        </el-button>
                        <el-button
                                @click="handleDelete(scope.row)"
                                :link="true"
                                style="color: #FFB101"
                        ><el-icon><Delete /></el-icon>删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <template v-if="list.length!=0">
                <el-pagination
                        :page-sizes="[5, 10, 15, 20]"
                        :total="total"
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        layout="total, sizes, prev, pager, next, jumper"
                        v-model:current-page="queryParams.pageNum"
                        v-model:page-size="queryParams.pageSize"
                />
            </template>
        </el-card>

        <el-drawer
                ref="drawerRef"
                v-model="dialog"
                title="饮食方案制定"
                direction="rtl"
                class="demo-drawer"
        >
            <div style="padding: 10px 20px">
                <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
                    <h3><el-icon><HomeFilled /></el-icon>健康管理平台</h3>
                    <p style="color: #42b983"><el-icon><CircleCheck /></el-icon>合理的饮食方案设计更利于健康的管理！</p>
                </el-card>

                <el-card class="box-card" style="text-align: left;line-height: 35px">
                    <el-form :model="form" ref="ruleFormRef" :rules="rules">
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="档案编号" prop="archiveNumber">
                                    <el-input v-model="form.archiveNumber" autocomplete="off" placeholder="识别用户信息" @blur="getArchivesByNumber()"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="12">
                                <el-form-item label="用户姓名">
                                    <el-input v-model="archive.userName" autocomplete="off" placeholder="编号输入后自动填入" :disabled="true"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="身份证号">
                                    <el-input v-model="archive.idCard" autocomplete="off" placeholder="编号输入后自动填入" :disabled="true"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="方案名称" prop="dietName">
                                    <el-input v-model="form.dietName" autocomplete="off" placeholder="请填写方案名称"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="方案内容" prop="dietContent">
                                    <el-input
                                            v-model="form.dietContent"
                                            :rows="5"
                                            type="textarea"
                                            placeholder="500字以内！"
                                    />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div style="padding: 10px 30px;position: relative;float: right;">
                        <el-button type="success" @click="submitForm">提交</el-button>
                    </div>
                </el-card>
            </div>
        </el-drawer>

        <el-drawer
                ref="drawerRef"
                v-model="dialog2"
                title="体检预约报告"
                direction="rtl"
                class="demo-drawer"
        >
            <div style="padding: 20px 20px">
                <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
                    <h3>体检预约报告</h3>
                    <p><el-icon><Location /></el-icon>xxx医院体检中心</p>
                    <p style="color: #42b983" v-if="detail.status==='1'"><el-icon><CircleCheck /></el-icon>你已成功预约体检，请准备好相关证件材料准时参加体检！</p>
                    <p style="color: #c20f2a" v-if="detail.status==='2'"><el-icon><CircleCheck /></el-icon>您的体检预约申请失败，您可选择其他时间再次预约！</p>
                </el-card>

                <el-card class="box-card" style="text-align: left;line-height: 35px">
                    <el-descriptions title="预约报告" column="1">
                        <el-descriptions-item label="体检编号"  v-if="detail.status==='1'"><h2>{{detail.appointmentNumber}}</h2></el-descriptions-item>
                        <el-descriptions-item label="体检人">{{detail.userName}}</el-descriptions-item>
                        <el-descriptions-item label="体检人">{{detail.userPhone}}</el-descriptions-item>
                        <el-descriptions-item label="证件号">{{detail.idCard}}</el-descriptions-item>
                        <el-descriptions-item label="预约时间">
                            {{ $filters.formatDate(detail.time) }}
                        </el-descriptions-item>
                    </el-descriptions>
                    <!--                    <div style="display: flex;justify-content: space-between;padding: 0px 30px">-->
                    <!--                        <el-button type="success" @click="submitForm">立即预约</el-button>-->
                    <!--                    </div>-->
                </el-card>
            </div>
        </el-drawer>

    </div>
</template>

<script>

    export default {
        data() {
            return {
                archive:{},//档案信息
                detail:{},
                dialog2:false,
                dialog:false,
                dialogFormVisible:false,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 用户表格数据
                list: [],
                // 弹出层标题
                title: "",
                // 表单参数
                form: {},
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    dietName: '',
                },
                roleOptions:[
                    {
                        label:"普通用户",
                        value:"U",
                    },
                    {
                        label:"管理员",
                        value: "A",
                    }
                ],
                // 表单校验
                rules: {
                    archiveNumber: [
                        {required: true, message: "请填写档案编号", trigger: "blur"},
                    ],
                    dietName: [
                        {required: true, message: "请填写方案名称", trigger: "blur"},
                        {min: 1, max: 20, message: '用户名称长度必须介于 1 和 20字 之间', trigger: 'blur'}
                    ],
                    dietContent: [
                        {required: true, message: "请填写方案内容", trigger: "blur"},
                        {min: 1, max: 500, message: '内容在500字以内！', trigger: 'blur'}
                    ],
                }
            };
        },
        created() {
            this.getUserList();
        },
        methods: {

            async getArchivesByNumber() {
                const {data: res} = await this.$http.get('archives/getArchivesByNumber', {
                    params: {
                        archiveNumber:this.form.archiveNumber,
                    }
                })
                if (res.code==200){
                    this.archive=res.data;
                }else{
                    this.$message.error("档案不存在！");
                }
            },

            keep(row){
                this.detail=row;
                this.dialog2=true;
            },

            addButton(){
                this.reset();
                this.dialog=true;
            },

            async getUserList() {
                const {data: res} = await this.$http.get('diet/getHealthDiets', {params:this.queryParams})
                console.log(res)
                this.list = res.data.children
                this.total = res.data.total
            },
            // 取消按钮
            cancel() {
                this.dialogFormVisible = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: 0,
                    dietName: "",
                    dietContent: "",
                    archiveNumber: "",
                };
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.getUserList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                // 查询参数
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    userPhone: "",
                },
                    this.getUserList();
            },
            /** 修改按钮操作 */
            async handleUpdate(row) {
                console.log(row)
                this.form = JSON.parse(JSON.stringify(row));
                this.title = "更新方案信息";
                this.dialog = true;
            },
            /** 提交按钮 */
            async submitForm() {
                await this.$refs["ruleFormRef"].validate(valid => {
                    if (valid) {
                        if (this.form.id != 0) {
                            this.modifyFrom();
                        } else {
                            this.addFromFun();
                        }
                    }
                });
                this.dialog = false;
            },
            /**修改用户*/
            async modifyFrom() {
                const {data: res} = await this.$http.post('diet/updateHealthDiet', this.form);
                if (res.code == 200) {
                    this.getUserList();
                    this.$message.success('修改成功!')
                } else {
                    this.$message.error('服务器异常')
                }
            },
            /**
             * 添加用户
             * */
            async addFromFun() {
                const {data: res} = await this.$http.post('diet/insertHealthDiet', this.form);
                console.log(res)
                if (res.code === 200) {
                    this.$message.success('方案添加成功！')
                    this.getUserList();
                } else {
                    this.$message.error('该档案已添加方案，你可进行方案更新！')
                }
            },
            /** 删除按钮操作 */
            async handleDelete(row) {
                this.$confirm('是否确定删除方案?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const {data: res} = await this.$http.get('diet/delHealthDiet', {params:{
                            id:row.id
                        }});
                    console.log(res)
                    if (res.code == 200) {
                        this.getUserList();
                        this.$message.success('删除成功！')
                    } else {
                        this.$message.error('服务器异常！')
                    }
                }).catch(() => {
                    this.$message.info('已取消删除！')
                })
            },
            handleSizeChange(newSize) {
                this.queryParams.pageSize = newSize;
                this.getUserList();
            }
            ,
            handleCurrentChange(current) {
                this.queryParams.pageNum = current;
                this.getUserList();
            },
        }
    };
</script>
